package fitness;

import java.text.DecimalFormat;

import model.OligoSystemGeneral;
import optimizers.ganeat.xor.ConstantsXOR;

import util.MyUtil;

public class RobustFastStrongOscillatorResult extends FitnessResult {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public OligoSystemGeneral standardModel;
	public FastStrongOscillatorResult[] results;
	public OligoSystemGeneral[] tests;

	@Override
	public double finalResult() {
		double result = 0;
		for (int i = 0; i < results.length; i++) {
			result += results[i].finalResult();
		}
		return result;
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("<html><body><h1>Fitness:");
		sb.append(common.Static.df4.format(finalResult()));
		sb.append("</h1>");
		sb.append("<table border=1><tr><th>Fitness</th><th>SeqK noisy values</th></tr>");
		for (int i = 0; i < results.length; i++) {
			sb.append("<tr><td>");
			sb.append((tests[i] == standardModel ? "*" : "")
					+ results[i].toString());
			sb.append("</td><td>");
			for (int j = 0; j < tests[i].nSimpleSequences; j++) {
				String color = "black";
				if (tests[i].seqK[0][j] > standardModel.seqK[0][j] * 1.2) {
					color = "red";
				} else if (tests[i].seqK[0][j] < standardModel.seqK[0][j] * 0.8) {
					color = "blue";
				}
				sb.append("[" + tests[i].intToName.get(j) + ":<font color="
						+ color + ">"
						+ common.Static.df4.format(tests[i].seqK[0][j])
						+ "</font>]");
			}
			for (int j = 0; j < tests[i].nSimpleSequences; j++) {
				for (int k = 0; k < tests[i].nSimpleSequences; k++) {
					if (standardModel.seqK[j + 1][k] != 0) {
						String color = "black";
						if (tests[i].seqK[j + 1][j] > standardModel.seqK[j + 1][k] * 1.2) {
							color = "red";
						} else if (tests[i].seqK[j + 1][k] < standardModel.seqK[j + 1][k] * 0.8) {
							color = "blue";
						}
						sb.append("[I"
								+ tests[i].intToName.get(j)
								+ tests[i].intToName.get(k)
								+ ":<font color="
								+ color
								+ ">"
								+ common.Static.df4
										.format(tests[i].seqK[j + 1][k])
								+ "</font>]");
					}
				}
			}
			sb.append("</td></tr>");
		}
		sb.append("</table>");
		sb.append("<p>StandDev: "
				+ common.Static.df4.format(standardDeviation()) + "</p>");
		return sb.toString();
	}

	@Override
	public Double[] getDecomposition() {
		// TODO Auto-generated method stub
		return null;
	}

	public double standardDeviation() {
		double avg = 0;
		for (int i = 0; i < results.length; i++) {
			avg += results[i].finalResult();
		}
		avg /= results.length;

		double sd = 0;
		for (int i = 0; i < results.length; i++) {
			sd += (results[i].finalResult() - avg)
					* (results[i].finalResult() - avg);
		}
		sd = Math.sqrt(sd);
		return sd;
	}

	@Override
	public boolean goodSolution() {
		// TODO Auto-generated method stub
		return false;
	}

}
